﻿using System;
using UnityEngine;
using UnityEngine.Networking;

public class Dodge : NetworkBehaviour
{
    private Collider co;
    public float distance = 2f;
    private int initialLayer = -1;
    private static int kCmdCmdDo = 0x418d74e2;
    private static int kRpcRpcDo;
    public Move move;

    static Dodge()
    {
        NetworkBehaviour.RegisterCommandDelegate(typeof(Dodge), kCmdCmdDo, new NetworkBehaviour.CmdDelegate(Dodge.InvokeCmdCmdDo));
        kRpcRpcDo = -182322376;
        NetworkBehaviour.RegisterRpcDelegate(typeof(Dodge), kRpcRpcDo, new NetworkBehaviour.CmdDelegate(Dodge.InvokeRpcRpcDo));
        NetworkCRC.RegisterBehaviour("Dodge", 0);
    }

    private void Awake()
    {
        this.co = base.GetComponentInParent<Collider>();
    }

    public void CallCmdDo()
    {
        if (!NetworkClient.active)
        {
            Debug.LogError("Command function CmdDo called on server.");
        }
        else if (base.isServer)
        {
            this.CmdDo();
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdDo);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            base.SendCommandInternal(writer, 0, "CmdDo");
        }
    }

    public void CallRpcDo()
    {
        if (!NetworkServer.active)
        {
            Debug.LogError("RPC Function RpcDo called on client.");
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 2));
            writer.WritePackedUInt32((uint) kRpcRpcDo);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            this.SendRPCInternal(writer, 0, "RpcDo");
        }
    }

    [Command]
    private void CmdDo()
    {
        this.CallRpcDo();
    }

    public void Do()
    {
        this.CallCmdDo();
    }

    protected static void InvokeCmdCmdDo(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            Debug.LogError("Command CmdDo called on client.");
        }
        else
        {
            ((Dodge) obj).CmdDo();
        }
    }

    protected static void InvokeRpcRpcDo(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkClient.active)
        {
            Debug.LogError("RPC RpcDo called on server.");
        }
        else
        {
            ((Dodge) obj).RpcDo();
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
    }

    private void OnEnable()
    {
        if (this.initialLayer == -1)
        {
            this.initialLayer = this.co.gameObject.layer;
        }
        this.trailGhost.enabled = false;
        this.co.gameObject.layer = this.initialLayer;
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        return flag;
    }

    [ClientRpc]
    private void RpcDo()
    {
        if ((!this.move.isForceMoving && (this.move.direction != Vector3.zero)) && this.isGrounded)
        {
            this.trailGhost.enabled = true;
            this.co.gameObject.layer = GameLayers.Effect.index;
            float duration = 0.1f;
            this.move.ForceMoveTo(base.transform.position + ((Vector3) (this.move.direction * this.distance)), duration, null);
            this.StartWaitForSeconds(duration, new Action(this.OnEnable));
        }
    }

    private void UNetVersion()
    {
    }

    public bool isGrounded
    {
        get
        {
            return Physics.Raycast(this.co.bounds.center, Vector3.down, (float) 1f, (int) GameLayers.Floor);
        }
    }

    private TrailGhost trailGhost
    {
        get
        {
            return this.move.trailGhost;
        }
    }
}

